package com.example.administrator.ygddriver.message;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.administrator.ygddriver.R;
import com.example.administrator.ygddriver.base.BaseActivity;
import com.example.administrator.ygddriver.base.BaseHint;
import com.example.administrator.ygddriver.base.UserBean;
import com.example.administrator.ygddriver.liteorm.LiteOrmTools;
import com.example.administrator.ygddriver.tool.OkGoRequests;
import com.example.administrator.ygddriver.tool.SimpleToolbar;
import com.example.administrator.ygddriver.tool.UrlValuse;
import com.example.administrator.ygddriver.webview.WebActivity;

import java.util.HashMap;

public class MessageActivity extends BaseActivity {
    private SimpleToolbar simpleToolbar;
    private RecyclerView recyclerView;
    private MessageAdapter messageAdapter;

    @Override
    public int initLayout() {
        return R.layout.activity_message;
    }

    @Override
    public void initView() {
        simpleToolbar = findViewById(R.id.activity_message_stb);
        recyclerView = findViewById(R.id.activity_message_rv);
        messageAdapter = new MessageAdapter(R.layout.item_activity_message, null);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        recyclerView.setAdapter(messageAdapter);
    }

    @Override
    public void initDate() {
        setSimpleToolbar();

        LiteOrmTools.getInformation(UserBean.class, this, new LiteOrmTools.On<UserBean>() {
            @Override
            public void hasData(UserBean data) {
                initQueryInformation(data.getC_token());
            }
        }, new LiteOrmTools.OFF() {
            @Override
            public void noData() {

            }
        });
        messageAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                QueryInformationRequestBean.ResultBean resultBean = (QueryInformationRequestBean.ResultBean) adapter.getData().get(position);
                Intent intent = new Intent(MessageActivity.this, WebActivity.class);
                String URL = UrlValuse.DOMAIN_NAME + UrlValuse.ZX_PLACE + resultBean.getC_id();
                intent.putExtra("URL", URL);
                startActivity(intent);

            }
        });
    }

    private void setSimpleToolbar() {
        simpleToolbar.setMainTitle("消息中心");
        simpleToolbar.setMainTitleColor("#000000");
        simpleToolbar.setBackIv(R.mipmap.left_title);
        simpleToolbar.setToolColor("#ffffff");
        simpleToolbar.setBackIvClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initQueryInformation(String c_token) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("c_token", c_token);
        hashMap.put("c_type", UrlValuse.TYPE);
        OkGoRequests.getInstance().putRequestAsync(UrlValuse.POST_API_CUSTOMER + UrlValuse.QUERY_INFORMATION, hashMap, QueryInformationRequestBean.class, new OkGoRequests.OnTrue<QueryInformationRequestBean>() {
            @Override
            public void hasData(QueryInformationRequestBean date) {
                if (date.isSuccess()) {
                    messageAdapter.setNewData(date.getResult());
                } else {
                    BaseHint baseHint = new BaseHint.addHint().setParameter(MessageActivity.this, "提示", date.getLocalizedMessage());
                    baseHint.show();
                }

            }
        }, new OkGoRequests.OnError<QueryInformationRequestBean>() {
            @Override
            public void noData() {

            }
        });
    }
}
